﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BusinessLogic.Security;
using Otgruzka.ServicesClients;

namespace Otgruzka.Controls.ExpendBill
{
  public partial class ExpendBillList : System.Web.UI.UserControl
  {
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.QueryString["oid"] != null)
        {
            int orderId = Convert.ToInt32(Request.QueryString["oid"]);
            var expendBills = new ExpendBillsClient().GetExpendBillList(UserPrincipal.Current.UserId, UserPrincipal.Current.OnliIts, orderId);
            if (expendBills != null)
            {
                this.ExpendBills.DataSource = expendBills.Select(b => b.ExpendBill).ToList();
                this.ExpendBills.DataBind();
            }
        }
    }

    protected void ExpendBills_SelectedIndexChanged(object sender, EventArgs e)
    {
        try
        {
            int id = Convert.ToInt32(ExpendBills.SelectedRow.Cells[0].Text);
            var orderId = Request.QueryString["oid"];
            Response.Redirect(string.Format("~/WebForms/ExpendBillDetails.aspx?oid={0}&exbid={1}", orderId, id));
        }
        catch (Exception ex)
        { }
    }


    /// <summary>
    /// Событие заполнения строки грида данными
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void ExpendBills_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        var orderId = Request.QueryString["oid"];
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            int expendedBillId = 0;
            var dataItem = e.Row.DataItem as DataTier.ExpendBill;
            if (dataItem != null)
            {
                expendedBillId = dataItem.Id;
            }

            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Attributes["onmouseover"] = "this.style.backgroundColor='#6b8cbe'";
                e.Row.Attributes["onmouseout"] = "this.style.backgroundColor='#FFFFFF'";
                e.Row.Attributes["style"] = "cursor:hand;";
                //if (expendedBillId > 0)
                //{
                //    try
                //    {
                //        string url = ResolveUrl(string.Format("~/WebForms/ExpendBillDetails.aspx?oid={0}&exbid={1}", orderId, expendedBillId));
                //        e.Row.Attributes["onClick"] = string.Format("location.href='{0}'", url);
                //    }
                //    catch (Exception ex)
                //    { }
                //}
                var grid = sender as GridView;
                string postback = Page.ClientScript.GetPostBackEventReference(grid, string.Format("Select${0}", e.Row.RowIndex));
                e.Row.Attributes.Add("onclick", postback.ToString());
            }
        }
    }


    /// <summary>
    /// Событие нажатия на LinkButton для редактирования Заказа
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void ExpendedBillEditButton_Click(object sender, EventArgs e)
    {
        try
        {
            var orderId = Request.QueryString["oid"];
            int expendedBillId = Convert.ToInt32((sender as LinkButton).CommandArgument);
            Response.Redirect(string.Format("~/WebForms/ExpendBillDetails.aspx?oid={0}&exbid={1}", orderId, expendedBillId));
        }
        catch (Exception ex)
        { }
    }


  }
}